package com.neu.api.controller.portal;

import com.neu.api.constant.StatusConst;
import com.neu.api.entity.School;
import com.neu.api.exception.ParamException;
import com.neu.api.properties.JWTProperties;
import com.neu.api.query.SchoolQuery;
import com.neu.api.service.SchoolService;
import com.neu.api.utils.JWTUtil;
import com.neu.api.utils.PageResult;
import com.neu.api.utils.Result;
import com.neu.api.utils.StringUtil;
import com.neu.api.vo.SchoolVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制层
 */
@RestController
@RequestMapping(value = "/admin/school")
public class SchoolRestController {
    @Autowired
    private SchoolService schoolService;

    @Resource
    private JWTProperties jwtProperties;

    //post  /admin/school
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<List<SchoolVO>> login(String loginName, String password) {
        //参数的校验
        if (StringUtil.isEmpty(loginName) || StringUtil.isEmpty(password)) {
            throw new ParamException("账号和密码都不能为空");
        }

        School school = schoolService.Login(loginName, password);
        if (school == null) {
            return Result.failure("账号或密码错误");
        }

        if (!Integer.valueOf(1).equals(school.getStatus())) {
            return Result.failure("账号被禁用");
        }

//        if (school.getStatus() == StatusConst.INACTIVE_STATUS) {
//            return Result.failure("账号被冻结");
//        }

        //服务器生成token --> 响应给前端----->前端每次发请求都携带token------->服务器要校验

        Map<String, Object> payload = new HashMap<>();
        payload.put(JWTUtil.USER_ID, school.getId());

        String token = JWTUtil.generateToken(payload, jwtProperties.getSecure(), jwtProperties.getExpire());

        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("userInfo", school);

        return Result.ok("成功", data);
    }


    @ApiOperation(value = "添加新的信息")


    @PostMapping
//    public boolean addSchool(@RequestBody SchoolVO schoolvo) {
    public boolean addSchool(SchoolVO schoolvo) {
        System.out.println("开始新增...");
        System.out.println("schoolvo ..."+schoolvo.toString());
        //name: 123
        //address: 1231
        //loginName: 321
        //linkName: 3213
        //linkPhone: 123213
        boolean b = schoolService.addSchool(schoolvo);
        return b;
    }

    // 这里只保存一个
    @ApiOperation(value = "更新信息")
//    @RequestMapping(value = "/school", method = RequestMethod.PUT)
    @PutMapping("")
    public boolean updateSchool(School school) {
        System.out.println("开始更新...");
        return schoolService.updateSchool(school);
    }

    @ApiOperation(value = "根据id删除信息")
//    @RequestMapping(value = "/school", method = RequestMethod.DELETE)
    @DeleteMapping("/{schoolId}")
    public boolean delete(@PathVariable(value = "schoolId", required = true) int schoolId) {
        System.out.println("开始删除...");
        return schoolService.deleteSchool(schoolId);
    }

    @ApiOperation(value = "根据姓名查询信息")
    @GetMapping(value = "/{schoolId}")
    public Result<School> findBySchoolName(@PathVariable(value = "schoolId", required = true) String schoolId) {
        System.out.println("开始查询...");
        return Result.ok("", schoolService.findSchoolById(schoolId));
    }

    @ApiOperation(value = "查询全部信息")
//    @ApiOperation("查询菜品")
    @GetMapping
    public Result<List<SchoolVO>> find(SchoolQuery schoolQuery) { //foodQuery封装了查询的条件
        PageResult<SchoolVO> page = schoolService.find(schoolQuery);
        return Result.ok("成功", page.getList(), page.getTotal());
    }
//    @RequestMapping(method = RequestMethod.GET)
//    @GetMapping
//    public List<SchoolVO> findBySchoolAge() {
//        Result sdf;
//
//        System.out.println("开始查询所有数据...");
//        List<School> schoolList = schoolService.findAll();
////        System.out.println("schoolList "+schoolList.toString());
//        for (School school : schoolList) {
//            System.out.println("sch "+school);
//            System.out.println("schname"+school.getName());
//        }
//        return schoolList ;
//    }

    @PostMapping("/{id}/{status}")
    public boolean updateStatus(@PathVariable(value = "id") Integer id,
                               @PathVariable(value = "status") Integer status){
        return schoolService.udpateStatus(id,status);
    }
}
